﻿package net.wangit.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ArrayUtil {

	private ArrayUtil() {
	}

	/**
	 * 从array数组中查找value值，反回value的索引。 找不到或参数为空时返回-1
	 */
	public static int find(String[] array, String value) {
		if (array == null || value == null)
			return -1;
		for (int i = 0; i < array.length; i++)
			if (value.equals(array[i]))
				return i;
		return -1;
	}

	/**
	 * 创建长度为length的String数据，并用str填充数组内每一项。
	 */
	public static String[] fillArray(String str, int length) {
		String[] result = new String[length];
		Arrays.fill(result, str);
		return result;
	}

	public static String[] toStringArray(Collection<Object> coll) {
		Object[] objects = coll.toArray();
		int length = objects.length;
		String[] result = new String[length];
		for (int i = 0; i < length; i++)
			result[i] = objects[i].toString();

		return result;
	}

	public static int[] toIntArray(Collection<Integer> coll) {
		Iterator<Integer> iter = coll.iterator();
		int arr[] = new int[coll.size()];
		int i = 0;
		while (iter.hasNext())
			arr[i++] = iter.next().intValue();
		return arr;
	}

	public static Object[] typecast(Object[] array, Object[] to) {
		return Arrays.asList(array).toArray(to);
	}

	/**
	 * 将数组转化为List
	 * 
	 * @param array:
	 *            例如String[]
	 * @return
	 */
	public static List<Object> toList(Object array) {
		if (array instanceof Object[])
			return Arrays.asList((Object[]) array);
		int size = Array.getLength(array);
		ArrayList<Object> list = new ArrayList<Object>(size);
		for (int i = 0; i < size; i++)
			list.add(Array.get(array, i));
		return list;
	}

	public static String[] slice(String strings[], int begin, int length) {
		String result[] = new String[length];
		for (int i = 0; i < length; i++)
			result[i] = strings[begin + i];

		return result;
	}

	public static Object[] slice(Object objects[], int begin, int length) {
		Object result[] = new Object[length];
		for (int i = 0; i < length; i++)
			result[i] = objects[begin + i];

		return result;
	}

	/**
	 * 将Iterator迭代转化为List
	 * 
	 * @param iter
	 * @return
	 */
	public static List<Object> toList(Iterator<Object> iter) {
		List<Object> list = new ArrayList<Object>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		return list;
	}

	public static String[] join(String x[], String y[]) {
		String result[] = new String[x.length + y.length];
		for (int i = 0; i < x.length; i++)
			result[i] = x[i];

		for (int i = 0; i < y.length; i++)
			result[i + x.length] = y[i];

		return result;
	}

	public static int[] join(int x[], int y[]) {
		int result[] = new int[x.length + y.length];
		for (int i = 0; i < x.length; i++)
			result[i] = x[i];

		for (int i = 0; i < y.length; i++)
			result[i + x.length] = y[i];

		return result;
	}

	public static String asString(Object[] os) {
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i = 0; i < os.length; i++) {
			sb.append(os[i]);
			if (i < os.length - 1)
				sb.append(",");
		}

		sb.append("]");
		return sb.toString();
	}

	public static boolean isAllNegative(int array[]) {
		for (int i = 0; i < array.length; i++)
			if (array[i] >= 0)
				return false;

		return true;
	}

	public static void addAll(Collection<Object> collection, Object array[]) {
		for (int i = 0; i < array.length; i++) {
			collection.add(array[i]);
		}
	}

	public static String[] cloneSubarray(String a[], int from, int to) {
		int n = to - from;
		String result[] = new String[n];
		System.arraycopy(a, from, result, 0, n);
		return result;
	}

	public static void print(String a[]) {
		if (a != null) {
			StringBuffer result = new StringBuffer("Array printer:\n");
			for (int i = 0; i < a.length; i++)
				result.append(
						StringUtil.fill(Integer.toString(i), 3, ' ', true))
						.append(a[i]).append("\n");

			System.out.print(result);
		} else {
			System.out.println("Array printer: null");
		}
	}

	public static void print(int a[]) {
		if (a != null) {
			StringBuffer result = new StringBuffer("Array printer:\n");
			for (int i = 0; i < a.length; i++)
				result.append(
						StringUtil.fill(Integer.toString(i), 3, ' ', true))
						.append(a[i]);

			System.out.println(result.toString());
		} else {
			System.out.println("Array printer: null");
		}
	}

	public static void print(String a[][]) {
		if (a != null) {
			StringBuffer result = new StringBuffer("Array printer:");
			for (int i = 0; i < a.length; i++) {
				result.append("\n").append(i).append(":");
				for (int j = 0; j < a[i].length; j++)
					result.append(" [").append(a[i][j]).append("]");

			}

			System.out.println(result.toString());
		} else {
			System.out.println("Array printer: null");
		}
	}

	public static String toString(Object arr[]) {
		if (arr == null)
			return "null";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			if (i > 0)
				sb.append(",");
			sb.append(arr[i]);
		}

		return sb.toString();
	}
}
